//
//  BMHDBManager+Area.m
//  BMHVideoMain
//
//  Created by 陈宪栋 on 2017/4/17.
//  Copyright © 2017年 Maoyx. All rights reserved.
//

#import "BMHDBManager+Area.h"
#import <FMDB.h>
#import <UtilsMacro.h>
#import "BMHAreaModel.h"

NSString *const kAreaTableName = @"area_info";
NSString *const kAreaId = @"area_id";
NSString *const kAreaParentId = @"parent_id";
NSString *const kAreaName = @"area_name";
NSString *const kAreaType = @"area_type";

@implementation BMHDBManager (Area)
/**
 *  获取本地数据库
 *
 *  @return 数据库对象
 */
-(FMDatabase *)getLocalDatabase{
    NSString *dbFilePath = [[NSBundle mainBundle] pathForResource:@"chinaArea.sqlite3" ofType:nil];
    FMDatabase *db= [FMDatabase databaseWithPath:dbFilePath] ;
    if ([db open]) {
        return db;
    }else{
        BMHLog(@"数据库打开失败");
        return nil;
    }
}

-(NSArray *)queryAddressWithParentId:(NSInteger)parentId{
    NSMutableArray *addressArray;
    FMDatabase *db = [self getLocalDatabase];
    if (db) {
        @try {
            NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ?", kAreaTableName, kAreaParentId];
            FMResultSet *result = [db executeQuery:querySql,[NSNumber numberWithInteger:parentId]];
            while ([result next]) {
                if (addressArray == nil) {
                    addressArray = [NSMutableArray array];
                }
                BMHAreaModel *model = [[BMHAreaModel alloc]init];
                model.areaId = [[result objectForColumnName:kAreaId] integerValue];
                model.parentId = [[result objectForColumnName:kAreaParentId] integerValue];
                model.areaName = [result stringForColumn:kAreaName];
                model.areaType = [[result objectForColumnName:kAreaType] integerValue];
                [addressArray  addObject:model];
            }
        }
        @catch (NSException *exception) {
            BMHLog(@"getAllAddressWithParentId : %@", exception);
        }
        @finally {
            [db close];
        }
    }
    return addressArray;
}

- (BMHAreaModel *)queryAddressWithAreaId:(NSInteger)areaId {
    BMHAreaModel *model;
    FMDatabase *db = [self getLocalDatabase];
    if (db) {
        @try {
            NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ?", kAreaTableName, kAreaId];
            FMResultSet *result = [db executeQuery:querySql,[NSNumber numberWithInteger:areaId]];
            while ([result next]) {
                if (model == nil) {
                    model = [[BMHAreaModel alloc]init];
                }
                model.areaId = [[result objectForColumnName:kAreaId] integerValue];
                model.parentId = [[result objectForColumnName:kAreaParentId] integerValue];
                model.areaName = [result stringForColumn:kAreaName];
                model.areaType = [[result objectForColumnName:kAreaType] integerValue];
            }
        }
        @catch (NSException *exception) {
            BMHLog(@"getAddressWithAreaId : %@", exception);
        }
        @finally {
            [db close];
        }
    }
    return model;
}

@end
